from filesystem import filesystem

class file_fake :
    def __init__ ( self , name , mode , readlines ) :
        self . _name = name
        self . _mode = mode
        self . _call_history = [ ]
        self . _readlines = readlines
    def writelines ( self , lines ) :
        self . _call_history += [ [ file_fake . writelines , lines ] ]
    def close ( self ) :
        self . _call_history += [ [ file_fake . close ] ]
    def readlines ( self ) :
        self . _call_history += [ [ file_fake . readlines ] ]
        return self . _readlines

class builtins_fake :
    def __init__ ( self ) :
        self . _files = [ ]
        self . _readlines = None
    def open ( self , name , mode ) :
        new_file = file_fake ( name , mode , self . _readlines )
        self . _files += [ new_file ]
        return new_file

class os_fake :
    def __init__ ( self ) :
        self . _removed = [ ]
        self . _listdir = None
        self . _listdir_path = None
    def remove ( self , path ) :
        self . _removed += [ path ]
    def listdir ( self , path ) :
        self . _listdir_path = path
        return self . _listdir

class mediator_fake :
    def __init__ ( self ) :
        self . _read_file_reply = None
        self . _list_files_reply = None
    def read_file_reply ( self , name , contents ) :
        self . _read_file_reply = [ name , contents ]
    def list_files_reply ( self , files ) :
        self . _list_files_reply = files

class filesystem_tests :
    'filesystem'
    def __init__ ( self , test ) :
        self . _test = test
        self . _mediator = mediator_fake ( )
        self . _builtins = builtins_fake ( )
        self . _os = os_fake ( )
        self . _filesystem = filesystem ( mediator = self . _mediator , builtins = self . _builtins , os = self . _os )
    def test_create_file ( self ) :
        self . _filesystem . create_file ( 'my file' , [ 'my first line' , 'my second line' ] )
        self . _test . assume_equal_len ( self . _builtins . _files , 1 )
        self . _test . assume_equal ( self . _builtins . _files [ 0 ] . _name , 'my file' )
        self . _test . assume_equal ( self . _builtins . _files [ 0 ] . _mode , 'w' )
        self . _test . assume_equal ( self . _builtins . _files [ 0 ] . _call_history ,
            [ [ file_fake . writelines , [ 'my first line\n' , 'my second line\n' ] ]
            , [ file_fake . close ]
            ] )
    def test_delete_file ( self ) :
        self . _filesystem . delete_file ( 'my file' )
        self . _test . assume_equal ( self . _os . _removed , [ 'my file' ] )
    def test_read_file ( self ) :
        self . _builtins . _readlines = \
            [ 'my first line\n'
            , 'my second line'
            , 'my third line\n'
            ]
        self . _filesystem . read_file_request ( 'my file' )
        self . _test . assume_equal ( self . _mediator . _read_file_reply , [ 'my file' ,
            [ 'my first line'
            , 'my second line'
            , 'my third line'
            ] ] )
        self . _test . assume_equal_len ( self . _builtins . _files , 1 )
        self . _test . assume_equal ( self . _builtins . _files [ 0 ] . _name , 'my file' )
        self . _test . assume_equal ( self . _builtins . _files [ 0 ] . _mode , 'r' )
        self . _test . assume_equal ( self . _builtins . _files [ 0 ] . _call_history ,
            [ [ file_fake . readlines ]
            , [ file_fake . close ]
            ] )
    def test_list_files ( self ) :
        self . _os . _listdir = tuple ( [ 'my first file' , 'my second file' ] )
        self . _filesystem . list_files_request ( )
        self . _test . assume_equal ( self . _mediator . _list_files_reply , [ 'my first file' , 'my second file' ] )
        self . _test . assume_equal ( self . _os . _listdir_path , '.' )
